package org.example.main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.example.domain.CustomerDO;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: WangShengJie
 * @Date: 2025/4/21
 */
public class FastJsonDemo {
    public static void main(String[] args) {
        CustomerDO customerDO1 = new CustomerDO();
        customerDO1.setName("周");
        customerDO1.setBirthday(Date.valueOf("1965-10-10"));

        CustomerDO customerDO2 = new CustomerDO();
        customerDO2.setName("刘");
        customerDO2.setBirthday(Date.valueOf("1976-10-03"));

        List listInput = new ArrayList();
        listInput.add(customerDO1);
        listInput.add(customerDO2);

        //fastjson 版本：1.2.58 使用方法1会报错
        //fastjson 版本：1.2.83 使用方法1不会报错
        //推荐使用方法2，无论fastjson什么版本，都不会报错
        String inputJson = JSON.toJSONString(listInput);
//        String inputJson = "[{\"birthday\":\"1965-10-10\",\"name\":\"周\"},{\"birthday\":\"1976-10-03\",\"name\":\"刘\"}]";
        System.out.println(inputJson);

        //方法1
        JSONArray jsonArray = JSON.parseArray(inputJson);
        List<CustomerDO> customerDOS1 = jsonArray.toJavaList(CustomerDO.class);
        System.out.println("方法1");
        for (CustomerDO customerDO : customerDOS1) {
            System.out.println(customerDO);
        }

        //方法2
        List<CustomerDO> customerDOS2 = JSON.parseArray(inputJson, CustomerDO.class);
        System.out.println("方法2");
        for (CustomerDO customerDO : customerDOS2) {
            System.out.println(customerDO);
        }
    }

}
